<template>
	<div class="container">
		<h3 class="header-title clearfix">
			<span></span>
			<p>{{headerTitle}}</p>
		</h3>
		<div class="add-account">
			<el-button type="danger" icon="el-icon-circle-plus-outline" @click="openAddAccountDialog">{{addAccountTitle}}</el-button>
		</div>
		<div class="search-account">
			<el-input v-model="phone" placeholder="请输入手机号"></el-input>
			<el-button type="primary" @click="searchPerson" icon="el-icon-search"></el-button>
		</div>
		<!-- 表格信息 -->
		<comTable :options="tableList" 
			@openLowerlist="openLowerlistDialog" @openModifyAccount="openModifyAccountDialog" @openDel="openDeleteDialog" @openProfitdetailed="openProfitdetailedDialog"
		></comTable>
		<!-- 新增或修改授权门店信息 -->
		<warAddSalesroom ref="salesroom" :options="salesroom"
			@addAccount="subAccountMsg" @modifyAccount="subAccountMsg"
		></warAddSalesroom>
		<!-- 删除操作弹窗 -->
		<praUnreported ref="del" :options="del" @subDel="submitDelete"></praUnreported>
		<!-- 下级账户授权及收益明细操作弹窗 -->
		<warProfitdetailedDialog @updataList="openLowerlistDialog" ref="profit" :options="profit"></warProfitdetailedDialog>
		
		<!-- 分页器 -->
		<div class="pageBox">
			<el-pagination
				@size-change="handleSizeChange"
				@current-change="pageChange"
				:current-page.sync="cur"
				:page-sizes="[10, 20, 30, 50, 100]"
				:page-size="pageSize"
				layout="total, sizes, prev, pager, next, jumper"
				:total="totalNum"
				background
			></el-pagination>
		</div>
	</div>
</template>

<script>
export default{
	components:{
		comTable: () => import("../common/comTable.vue"),
		warAddSalesroom: () => import("../common/warAddSalesroom.vue"),
		praUnreported: () => import("../common/praUnreported.vue"),
		warProfitdetailedDialog: () => import("../common/warProfitdetailedDialog.vue")
	},
	watch:{
		'$store.getters.getCurBranchId'(){
			console.log(JSON.parse(localStorage.getItem('userinfo')).curBranchId);
			if(this.userinfo.grade === '11'){
				this.searchStaff();
			}
		}
	},
	mounted(){
		console.log(JSON.parse(localStorage.getItem('userinfo')).curBranchId);
		this.userinfo = JSON.parse(localStorage.getItem('userinfo'));
		if(this.userinfo.grade === '10' || this.userinfo.grade === '13'){
			this.headerTitle = '线下辅导站管理';
			this.addAccountTitle = '新增门店负责人';
			this.salesroom.level = '10';
			this.searchOutlets();
		}else if(this.userinfo.grade === '11'){
			this.headerTitle = '操作员账户管理';
			this.addAccountTitle = '新增下级账户';
			this.salesroom.level = '11';
			this.tableList = {
				list:[
					{title:'姓名',prop:'staffName',isClick:false},
					{title:'手机号',prop:'staffPhone',isClick:false}
				],
				noOperation:true,	//是否有操作项
				operation:[			//操作项内容
					{name:'删除',isBtn:false,title:'openDel'}
				],
				data:[				//请求到的表格数据
					/* {
						staffName: '老王',						//负责人姓名
						staffPhone: '16666666666',				//手机号
					},{
						staffName: '老王',						//负责人姓名
						staffPhone: '16666666666',				//手机号
					} */
				]
			};
			this.searchStaff();
		}
	},
	data(){
		return{
			userinfo:'',			//登录者的用户信息
			phone:'',				//绑定输入手机号搜索
			headerTitle:'',			//头部标题
			addAccountTitle:'',		//新增负责人标题
			curBranchId: '',		//当前需要修改网点信息的网点id
			delMsg:{
				delType:'',				//删除的账户类型：principalAccount负责人 operator操作员
				branchId:'',			//需要删除的门店ID
				subordinateId:''		//需要删除的门店操作员ID
			},
			tableList:{
				list:[
					{title:'线下门店名称',prop:'branchName',isClick:false},
					{title:'负责人姓名',prop:'principalName',isClick:false},
					{title:'门店区域',prop:'principalAccount',isClick:false},
					{title:'手机号',prop:'principalPhone',isClick:false},
					{title:'授权时间',prop:'createTime',isClick:false},
					{title:'下级授权账号',prop:'authorizedaccount',isClick:true,eventName:'openLowerlist'}
				],
				noOperation:true,	//是否有操作项
				operation:[			//操作项内容
					{name:'修改',isBtn:false,title:'openModifyAccount'},
					{name:'删除',isBtn:false,title:'openDel'},
					{name:'收益明细',isBtn:false,title:'openProfitdetailed'},
				],
				data:[				//请求到的表格数据
					/* {
						branchName: '徐汇门店',					//线下门店名称
						principalName: '老王',						//负责人姓名
						storearea: '上海市-徐汇康健',			//门店区域
						principalPhone: '16666666666',				//手机号
						createTime: '2016-05-02 11:30',		//授权时间
						authorizedaccount: '查看',				//下级授权账号
					} */
					/* {
						branchId: "213bceb3005d488b8ea21f433ef832ab",
						branchName: "总店",
						createTime: "2020-05-18 14:12:41.0",
						deleted: "1",
						principalAccount: "fc666666",
						principalName: "谢镔",
						principalPassword: "admin@123",
						principalPhone: "15202120781",
						updateTime: "2020-05-18 14:12:44.0",
						userId: "88514e7096034ac8a43565a5aecea919",
						userName: "谢镔",
					} */
				]
			},
			salesroom:{
				type:'',				//弹窗的类型，新增或者是修改
				title:'',				//弹窗的标题，新增或者是修改
				eventName:'',			//提交事件的名称
				level:'',
				accountMsg:{}			//当前需要修改的负责人的信息
			},
			del:{
				title:'删除',
				linefeedPre:'确认是否删除该门店负责人账户',
				linefeedNext:'',
				subCon:'确认并删除',
				eventName:'subDel',
				msg:{}
			},
			profit:{
				title:'收益明细-上海市徐汇区徐汇门店',
				principalMsg:{},
				totalNum:0,					//列表总共有多少条数据
				tableList:{
					list:[
						{title:'招聘岗位',prop:'jobname',isClick:false},
						{title:'薪资区间',prop:'salary',isClick:false},
						{title:'发布时间',prop:'releasetime',isClick:false},
						{title:'区域',prop:'storearea',isClick:false},
						{title:'支付金额',prop:'paymoney',isClick:false},
						{title:'收益金额',prop:'profitmoney',isClick:false},
						{title:'支付状态',prop:'paystatus',isClick:false}
					],
					noOperation:false,	//是否有操作项
					operation:[			//操作项内容
						
					],
					data:[				//请求到的表格数据
						{
							jobname: '月嫂',						//招聘岗位
							salary: '6k-7k',						//薪资区间
							releasetime: '2016-05-02 11:30',		//发布时间
							storearea: '上海市-徐汇区七莘路1008号',	//区域
							paymoney: '1400',						//支付金额
							profitmoney: '待雇主支付',				//收益金额
							paystatus: '待支付',						//支付状态
						},{
							jobname: '月嫂',						//招聘岗位
							salary: '6k-7k',						//薪资区间
							releasetime: '2016-05-02 11:30',		//发布时间
							storearea: '上海市-徐汇区七莘路1008号',	//区域
							paymoney: '1400',						//支付金额
							profitmoney: '待雇主支付',				//收益金额
							paystatus: '待支付',						//支付状态
						},{
							jobname: '月嫂',						//招聘岗位
							salary: '6k-7k',						//薪资区间
							releasetime: '2016-05-02 11:30',		//发布时间
							storearea: '上海市-徐汇区七莘路1008号',	//区域
							paymoney: '1400',						//支付金额
							profitmoney: '待雇主支付',				//收益金额
							paystatus: '待支付',						//支付状态
						}
					]
				},
			},
			
			/* 分页功能数据 */
			cur:1,						//当前是第几页
			pageSize: 10,				//每页有多少条数据
			totalNum: 0,				//总共有多少条数据,根据这个数据去适配页数
		}
	},
	methods:{
		// 打开新增下级账户弹窗
		openAddAccountDialog(){
			if(this.userinfo.grade === '10' || this.userinfo.grade === '13'){
				this.salesroom.title = '新增门店负责人';
			}else{
				this.salesroom.title = '新增下级账户';
			}
			this.salesroom.type = 'add';
			this.salesroom.eventName = 'addAccount';
			this.$refs.salesroom.open()
		},
		// 提交新增或修改信息弹窗
		subAccountMsg(data){
			console.log(data);
			console.log(this.userinfo);
			let params = {
				branchName: data.branchName,
				userName: this.userinfo.name,
				userId: '88514e7096034ac8a43565a5aecea919',
				address: data.adress,
				province:data.province,
				city:data.city,
				county:data.county,
				principalName:data.principalName,
				principalPhone:data.principalPhone,
				principalAccount:data.principalAccount,
				assessment:data.assessment
			};
			if((this.userinfo.grade === '10' || this.userinfo.grade === '13') && data.branchName && data.principalName && data.principalPhone && data.county && data.province && data.city && data.adress && data.principalAccount && data.assessment.length>0){
				//总账户新增负责人
				if(this.salesroom.type === 'add'){
					params.branchId = '';
					console.log(params);
					this.$houseAxios.post('/housekeeping/Branch/insertOrUpdateBranch',params).then(res => {
						console.log(res);
						if(res.data.code === 1000){
							this.$message({
							  message: '新增成功',
							  type: 'success'
							})
							this.searchOutlets();
						}else{
							this.$message({
							  message: res.data.message,
							  type: 'warning'
							})
						}
					})
				}else{
					//总账户修改负责人信息
					params.branchId = this.curBranchId;
					console.log(params);
					this.$houseAxios.post('/housekeeping/Branch/insertOrUpdateBranch',params).then(res => {
						console.log(res);
						if(res.data.code === 1000){
							this.$message({
								message: '负责人信息修改成功',
								type:'success'
							})
							this.searchOutlets();
						}
					})
					
				}
				
				this.$refs.salesroom.close();
			}else if(this.userinfo.grade === '11' && data.staffName && data.staffPhone && data.staffAccount){
				// 门店负责人新增下级操作员
				let params = {
					staffName: data.staffName,
					staffAccount: data.staffAccount,
					staffPhone: data.staffPhone,
					principalAccount: this.userinfo.account,
					branchId: JSON.parse(localStorage.getItem('userinfo')).curBranchId
				}
				console.log(params);
				this.$houseAxios.post('/housekeeping/Subordinate/insertSubordinate',params).then(res => {
					console.log(res);
					if(res.data.code === 1000){
						this.$message({
						  message: "添加成功",
						  type: "success"
						});
						this.searchStaff();
					}
				})
				
				this.$refs.salesroom.close();
			}else{
				this.$message({
				  message: "请将信息填写完整",
				  type: "warning"
				});
			}
			
		},
		//查询线下的网点列表
		searchOutlets(){
			this.tableList.data = [];
			let params = {
				pageNum: this.cur,
				pageSize: this.pageSize
			};
			if(this.phone){
				params.principalPhone = this.phone;
			}
			this.$houseAxios.post('/housekeeping/Branch/selectBranchList',params).then(res => {
				console.log(res);
				if(res.data.code === 1000){
					let result = res.data.data;
					this.totalNum = parseInt(result.totalNum);
					result.list.forEach(item => {
						item.createTime = item.createTime.substring(0,item.createTime.length-2);
						this.tableList.data.push(item);
					})
				}
			});
		},
		//查询线下门店操作员列表
		searchStaff(){
			this.tableList.data = [];
			
			let params = {
				branchId: JSON.parse(localStorage.getItem('userinfo')).curBranchId,
				pageNum:this.cur,
				pageSize:this.pageSize
			}
			if(this.phone){
				params.staffPhone = this.phone;
			};
			console.log(params);
			this.$houseAxios.post('/housekeeping/Subordinate/selectSubordinateList',params).then(res => {
				console.log(res);
				if(res.data.code === 1000){
					let result = res.data.data;
					this.totalNum = parseInt(result.totalNum);
					result.list.forEach(item => {
						this.tableList.data.push(item);
					})
				}
			})
		},
		
		//根据手机号查询负责人或者是线下操作员
		searchPerson(){
			if(this.phone == ''){
				this.$message({
					message: '手机号不能为空',
					type: 'warning'
				});
				return 
			}
			if(!(/^1[3456789]\d{9}$/.test(this.phone))){
				this.$message({
					message: '手机号码格式有误，请重新填写！',
					type: 'warning'
				})
				return
			}
			this.tableList.data = [];
			if(this.userinfo.grade === '10' || this.userinfo.grade === '13'){
				let params = {
					pageNum: this.cur,
					pageSize: this.pageSize,
					principalPhone: this.phone
				}
				this.$houseAxios.post('/housekeeping/Branch/selectBranchList',params).then(res => {
					if(res.data.code === 1000){
						let result = res.data.data;
						this.totalNum = parseInt(result.totalNum);
						result.list.forEach(item => {
							item.createTime = item.createTime.substring(0,item.createTime.length-2);
							this.tableList.data.push(item);
						})
					}
				})
			}else if(this.userinfo.grade === '11'){
				let params = {
					pageNum: this.cur,
					pageSize: this.pageSize,
					staffPhone: this.phone,
					branchId: JSON.parse(localStorage.getItem('userinfo')).curBranchId
				};
				this.$houseAxios.post('/housekeeping/Subordinate/selectSubordinateList',params).then(res => {
					console.log(res);
					if(res.data.code === 1000){
						let result = res.data.data;
						this.totalNum = parseInt(result.totalNum);
						result.list.forEach(item => {
							this.tableList.data.push(item);
						})
					}
				})
			}
			
		},
		// 打开修改负责人账户信息弹窗
		openModifyAccountDialog(data){
			console.log(data);
			this.salesroom.type = 'modify';
			this.salesroom.accountMsg = data;
			this.salesroom.title = '门店负责人信息修改';
			this.salesroom.eventName = 'modifyAccount';
			this.curBranchId = data.branchId;
			this.$refs.salesroom.open()
		},
		// 打开下级授权列表
		openLowerlistDialog(index){
			console.log(index);
			this.profit.title = "下级授权账户";
			this.profit.principalMsg = index;
			this.profit.tableList = {
				list:[
					{title:'姓名',prop:'staffName',isClick:false},
					{title:'手机号',prop:'staffPhone',isClick:false},
				],
				noOperation:true,	//是否有操作项
				operation:[			//操作项内容
					{name:'删除',isBtn:false,title:'delAccount'},
				],
				data:[				//请求到的表格数据
					/* {
						staffName: '老王',						//姓名
						staffPhone: '13333333333',				//手机号
					},{
						staffName: '老王',						//姓名
						staffPhone: '13333333333',				//手机号
					},{
						staffName: '老王',						//姓名
						staffPhone: '13333333333',				//手机号
					}, */
				]
			};
			this.$houseAxios.post('/housekeeping/Subordinate/selectSubordinateList',{branchId:this.profit.principalMsg.branchId,pageNum:1}).then(res => {
				console.log(res);
				if(res.data.code === 1000){
					let result = res.data.data;
					this.profit.totalNum = parseInt(result.totalNum);
					result.list.forEach(item => {
						this.profit.tableList.data.push(item);
					})
					
				}
				
			})
			
			this.$refs.profit.open();
		},
		// 打开删除操作弹窗
		openDeleteDialog(index){
			console.log(index);
			this.del.msg = index;
			if(index.assessmentAuthority != undefined){
				this.delMsg.delType = 'principalAccount';
				this.delMsg.branchId = index.branchId;
			}else{
				this.delMsg.delType = 'operator';
				
			}
			this.$refs.del.open();
		},
		// 提交删除数据操作
		submitDelete(){
			//删除门店及负责人
			if(this.delMsg.delType == 'principalAccount'){
				this.$houseAxios.post('/housekeeping/Branch/deleteBranch',{
					branchId:this.delMsg.branchId
				}).then(res => {
					console.log(res);
					if(res.data.code === 1000){
						this.$message({
							message: res.data.message,
							type:'success'
						});
						this.searchOutlets();
					}
				})
			//删除门店的线下操作员	
			}else if(this.delMsg.delType == 'operator'){
				this.$houseAxios.post('/housekeeping/Subordinate/deleteSubordinate',{subordinateId:this.del.msg.subordinateId}).then(res => {
					console.log(res);
					if(res.data.code === 1000){
						this.$message({
							message: res.data.message,
							type:'success'
						});
						this.searchStaff();
					}
				})
			}
			
			
			this.$refs.del.close();
		},
		// 打开收益明细弹窗
		openProfitdetailedDialog(index){
			console.log(index);
			this.profit.title = '收益明细-上海市徐汇区徐汇门店';
			this.profit.tableList = {
				list:[
					{title:'招聘岗位',prop:'jobTitle',isClick:false},
					{title:'薪资区间',prop:'salary',isClick:false},
					{title:'发布时间',prop:'createTime',isClick:false},
					{title:'区域',prop:'address',isClick:false},
					{title:'支付金额',prop:'aggregateScore',isClick:false},
					{title:'收益金额',prop:'profitmoney',isClick:false},
					{title:'支付状态',prop:'restoreIntegral',isClick:false}
				],
				noOperation:false,	//是否有操作项
				operation:[			//操作项内容
					
				],
				data:[				//请求到的表格数据
					/* {
						jobTitle: '月嫂',							//招聘岗位
						salary: '6k-7k',							//薪资区间
						createTime: '2016-05-02 11:30',				//发布时间
						address: '上海市-徐汇区七莘路1008号',			//区域
						aggregateScore: '1400',						//支付金额
						profitmoney: '待雇主支付',					//收益金额
						restoreIntegral: '待支付',					//支付状态
					} */
				]
			};
			let params = {
				branchId: index.branchId,
				pageNum: this.cur,
				pageSize: this.pageSize
			};
			this.$houseAxios.post('/housekeeping/Branch/selectPosotionBranch',params).then(res => {
				console.log(res);
				if(res.data.code === 1000){
					let result = res.data.data;
					this.profit.totalNum = parseInt(result.totalNum);
					result.list.forEach(item => {
						if(item.restoreIntegral == '待支付'){
							item.profitmoney = '待支付';
						}else{
							item.profitmoney = item.aggregateScore;
						}
						item.createTime = item.createTime.substring(0,item.createTime.length-2);
						item.salary = item.payFloor + '~' + item.payUpper;
						this.profit.tableList.data.push(item);
					})
				}
			})
			
			this.$refs.profit.open();
		},
		
		//当前页是第几页,点击的第几页的时候调用这个函数
		pageChange(p) {
			// 点击的时候传过来一个参数，这个参数就是所点击的当前页数，
			// 然后把这个当前页数转换成字符串形式赋值给page，
			// 然后把page这个参数传给search这个方法
			// let page = p.toString();
			console.log(p);
			if(this.userinfo.grade === '10' || this.userinfo.grade === '13'){
				this.searchOutlets();
			}else if(this.userinfo.grade === '11'){
				this.searchStaff();
			}
			
		},
		//每页有多少条,10,20,50等,点击选择的时候调用这个方法
		handleSizeChange (val) {
			console.log(val);
			this.pageSize = val;
			if(this.userinfo.grade === '10' || this.userinfo.grade === '13'){
				this.searchOutlets();
			}else if(this.userinfo.grade === '11'){
				this.searchStaff();
			}
		},
	}
}
</script>

<style scoped="scoped">
.container{
	background-color: #fff;
	height: 100%;
	padding-left: 20px;
}
.header-title{
	padding: 0 0 20px 0;
	border-bottom: 1px solid #D2D2D2;
}
.header-title span{
	width: 15px;
	height: 30px;
	background-color: #0064E4;
	margin-right: 20px;
	float: left;
}
.header-title p{
	float: left;
	font-weight: bold;
	line-height: 30px;
}
.pageBox{
	margin-top: 20px;
}
.add-account,.search-account{
	margin-top: 20px;overflow: hidden;
}
</style>
